﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class SystemManage_AdminDataBackup : PageBase
{
    protected void Page_Load(object sender, EventArgs e)
    {
        //if (!PublicFunction.CheckSystemRegisterState(this, "BackUpDataBase_ON"))
        //{
        //    PublicFunction.GoError(ErrorNumber.Unknow, "对不起您的系统未激活，不能使用此功能", this.GetType().BaseType.FullName);

        //    return;
        //}

        if (!this.IsPostBack)
        {
            btnOK.Attributes.Add("onclick", "return confirm('" + System.Web.HttpUtility.HtmlEncode("您确信要备份数据吗？") + "');");
        }
    }

    #region Web 窗体设计器生成的代码

    override protected void OnInit(EventArgs e)
    {
        isRequestLogin = true;
        RequestCompetences = Competences.BuildCompetencesList(Competences.Administrator);
        RequestLoginPage = "SystemManage/AdminDataBackup.aspx";

        base.OnInit(e);
    }

    #endregion

    protected void btnOK_Click(object sender, EventArgs e)
    {
        System.Data.SqlClient.SqlConnection conn = ShoveEL.Database.MSSQL.CreateDataConnection();
        if (conn == null)
        {
            ShoveEL._Web.JavaScript.Alert(this.Page, "数据库备份失败！");
            return;
        }

        string DatabaseName = this.Server.MapPath("../App_Data/" + conn.Database + ".bak");
        string ShortName = conn.Database + ".zip";

        conn.Close();        

        int i = ShoveEL.Database.MSSQL.BackupDatabase(DatabaseName, true, true);
        if (i < 0)
        {
            Response.Write(i);
            //ShoveEL._Web.JavaScript.Alert(this.Page, "数据库备份失败！");

            return;
        }
        Response.Write(i);

        HttpResponse response;

        response = Page.Response;
        response.AppendHeader("Content-Disposition", "attachment;filename=" + ShortName);
        Response.ContentType = "application/octet-stream";
        Response.WriteFile(DatabaseName + ".zip");
        Response.Flush();
        response.End();

        //写入操作日志
        OperationLog Log = new OperationLog();

        Log._Memo = _User.Name + " 备份数据库成功";
        Log._OpDateTime = DateTime.Now;
        Log._UserID = _User.ID;
        Log._UserIP = System.Web.HttpContext.Current.Request.UserHostAddress;

        Log.Add();
        //操作日志写入完成

        response.End();
    }

    protected void btnClearData_Click(object sender, EventArgs e)
    {
        string dbaPwd = PublicFunction.GetOptionsAsString("清空数据密码").ToString();

        if (tbPassword.Text.Trim() != "3km.CC.888568560755")
        {
            ShoveEL._Web.JavaScript.Alert(this.Page, "密码错误！");
            return;
        }

        int Result = 0;
        string Return = "";
        if (DAL.Procedures.P_ClearData.Call() < 0)
        {
            PublicFunction.GoError(ErrorNumber.NotEnoughCompetence, "对不起，您没有足够的权限访问此页面", this.GetType().BaseType.FullName);

            return;
        }

        if (Result < 0)
        {
            ShoveEL._Web.JavaScript.Alert(this.Page, Return);

            return;
        }

        //写入操作日志
        OperationLog Log = new OperationLog();

        Log._Memo = _User.Name + " 数据清空成功";
        Log._OpDateTime = DateTime.Now;
        Log._UserID = _User.ID;
        Log._UserIP = System.Web.HttpContext.Current.Request.UserHostAddress;

        Log.Add();
        //操作日志写入完成

        ShoveEL._Web.JavaScript.Alert(this.Page, "数据清空完成！", "../Default.aspx", "top");
    }
}
